package com.csit.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.entity.Buy;
import com.csit.entity.DataDictionary;
import com.csit.entity.Sale;
import com.csit.entity.Wastage;
import com.csit.service.BuyDetailService;
import com.csit.service.SaleDetailService;
import com.csit.service.WastageService;
import com.csit.util.DateUtils;
import com.csit.util.ListAndJsonUtil;

@ParentPackage("basePackage")
public class StatisticsAction extends BaseAction {
	
	private static final long serialVersionUID = 4464167668285048076L;
	
	//private static final Integer[] MONTHDAY
	
	private String beginDate;
	
	private String endDate;
	
	private Integer shopID;

	@Resource
	private BuyDetailService buyDetailService;
	
	@Resource
	private SaleDetailService saleDetailService;
	
	@Resource
	private WastageService wastageService;
	
	public void countByDay(){
		Date date = null;
		if(beginDate==null){
			date = new Date();
		}else{
			date = DateUtils.getDate(beginDate);
		}
		Buy buy = new Buy();
		Sale sale = new Sale();
		Wastage wastage = new Wastage();
		if(shopID!=null){
			DataDictionary shop = new DataDictionary();
			buy.setShop(shop);
			sale.setShop(shop);
			wastage.setShop(shop);
		}
		Map<String, Double> buyMap = buyDetailService.countBuyDetail(buy, date, date);
		Map<String, Double> saleMap = saleDetailService.countSaleDetail(sale, date, date);
		Map<String, Double> wastageMap = wastageService.countWastage(wastage, date, date);
		
		
		Double buyAmount = buyMap.get("totalAmount");
		Double saleAmount = saleMap.get("totalAmount");
		Double wasteageAmount = wastageMap.get("totalAmount");
		buyAmount = buyAmount==null?0d:buyAmount;
		saleAmount = saleAmount==null?0d:saleAmount;
		wasteageAmount = wasteageAmount==null?0d:wasteageAmount;
		
		Double total = saleAmount-buyAmount-wasteageAmount;
		
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		
		Map<String,Object> buymap = new HashMap<String, Object>();
		buymap.put("name", "采购费用");
		buymap.put("value", buyAmount);
		
		Map<String,Object> salemap = new HashMap<String, Object>();
		salemap.put("name", "销售费用");
		salemap.put("value", saleAmount);
		
		Map<String,Object> wastagemap = new HashMap<String, Object>();
		wastagemap.put("name", "损耗费用");
		wastagemap.put("value", wasteageAmount);
		
		Map<String,Object> totalmap = new HashMap<String, Object>();
		totalmap.put("name", "总计");
		totalmap.put("value", total);
		
		listMap.add(buymap);
		listMap.add(wastagemap);
		listMap.add(salemap);
		listMap.add(totalmap);
		
		String jsonString = ListAndJsonUtil.getJSONStringFromListMap(listMap);
		
		ajaxJson(jsonString);
	}
	
	public void countByMonth(){
		Date date = null;
		Date beginTime = null;
		Date endTime = null;
		try {
			date = DateUtils.getDateFromString(beginDate, "yyyy-MM");
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			
			beginTime = DateUtils.getDate(beginDate+"-01");
			endTime = DateUtils.getDate(beginDate+"-"+calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Buy buy = new Buy();
		Sale sale = new Sale();
		Wastage wastage = new Wastage();
		if(shopID!=null){
			DataDictionary shop = new DataDictionary();
			buy.setShop(shop);
			sale.setShop(shop);
			wastage.setShop(shop);
		}
		Map<String, Double> buyMap = buyDetailService.countBuyDetail(buy, beginTime, endTime);
		Map<String, Double> saleMap = saleDetailService.countSaleDetail(sale, beginTime, endTime);
		Map<String, Double> wastageMap = wastageService.countWastage(wastage, beginTime, endTime);
		
		
		Double buyAmount = buyMap.get("totalAmount");
		Double saleAmount = saleMap.get("totalAmount");
		Double wasteageAmount = wastageMap.get("totalAmount");
		buyAmount = buyAmount==null?0d:buyAmount;
		saleAmount = saleAmount==null?0d:saleAmount;
		wasteageAmount = wasteageAmount==null?0d:wasteageAmount;
		
		Double total = saleAmount-buyAmount-wasteageAmount;
		
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		
		Map<String,Object> buymap = new HashMap<String, Object>();
		buymap.put("name", "采购费用");
		buymap.put("value", buyAmount);
		
		Map<String,Object> salemap = new HashMap<String, Object>();
		salemap.put("name", "销售费用");
		salemap.put("value", saleAmount);
		
		Map<String,Object> wastagemap = new HashMap<String, Object>();
		wastagemap.put("name", "损耗费用");
		wastagemap.put("value", wasteageAmount);
		
		Map<String,Object> totalmap = new HashMap<String, Object>();
		totalmap.put("name", "总计");
		totalmap.put("value", total);
		
		listMap.add(buymap);
		listMap.add(wastagemap);
		listMap.add(salemap);
		listMap.add(totalmap);
		
		String jsonString = ListAndJsonUtil.getJSONStringFromListMap(listMap);
		
		ajaxJson(jsonString);
	}
	
	public void countByYear(){
		Date beginTime = DateUtils.getDate(beginDate+"-01-01");;
		Date endTime = DateUtils.getDate(beginDate+"-12-31");
		
		Buy buy = new Buy();
		Sale sale = new Sale();
		Wastage wastage = new Wastage();
		if(shopID!=null){
			DataDictionary shop = new DataDictionary();
			buy.setShop(shop);
			sale.setShop(shop);
			wastage.setShop(shop);
		}
		Map<String, Double> buyMap = buyDetailService.countBuyDetail(buy, beginTime, endTime);
		Map<String, Double> saleMap = saleDetailService.countSaleDetail(sale, beginTime, endTime);
		Map<String, Double> wastageMap = wastageService.countWastage(wastage, beginTime, endTime);
		
		
		Double buyAmount = buyMap.get("totalAmount");
		Double saleAmount = saleMap.get("totalAmount");
		Double wasteageAmount = wastageMap.get("totalAmount");
		buyAmount = buyAmount==null?0d:buyAmount;
		saleAmount = saleAmount==null?0d:saleAmount;
		wasteageAmount = wasteageAmount==null?0d:wasteageAmount;
		
		Double total = saleAmount-buyAmount-wasteageAmount;
		
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		
		Map<String,Object> buymap = new HashMap<String, Object>();
		buymap.put("name", "采购费用");
		buymap.put("value", buyAmount);
		
		Map<String,Object> salemap = new HashMap<String, Object>();
		salemap.put("name", "销售费用");
		salemap.put("value", saleAmount);
		
		Map<String,Object> wastagemap = new HashMap<String, Object>();
		wastagemap.put("name", "损耗费用");
		wastagemap.put("value", wasteageAmount);
		
		Map<String,Object> totalmap = new HashMap<String, Object>();
		totalmap.put("name", "总计");
		totalmap.put("value", total);
		
		listMap.add(buymap);
		listMap.add(wastagemap);
		listMap.add(salemap);
		listMap.add(totalmap);
		
		String jsonString = ListAndJsonUtil.getJSONStringFromListMap(listMap);
		
		ajaxJson(jsonString);
	}
	
	public void count(){
		Date beginTime = DateUtils.getDate(beginDate);;
		Date endTime = DateUtils.getDate(endDate);
		
		Buy buy = new Buy();
		Sale sale = new Sale();
		Wastage wastage = new Wastage();
		if(shopID!=null){
			DataDictionary shop = new DataDictionary();
			buy.setShop(shop);
			sale.setShop(shop);
			wastage.setShop(shop);
		}
		Map<String, Double> buyMap = buyDetailService.countBuyDetail(buy, beginTime, endTime);
		Map<String, Double> saleMap = saleDetailService.countSaleDetail(sale, beginTime, endTime);
		Map<String, Double> wastageMap = wastageService.countWastage(wastage, beginTime, endTime);
		
		
		Double buyAmount = buyMap.get("totalAmount");
		Double saleAmount = saleMap.get("totalAmount");
		Double wasteageAmount = wastageMap.get("totalAmount");
		buyAmount = buyAmount==null?0d:buyAmount;
		saleAmount = saleAmount==null?0d:saleAmount;
		wasteageAmount = wasteageAmount==null?0d:wasteageAmount;
		
		Double total = saleAmount-buyAmount-wasteageAmount;
		
		List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
		
		Map<String,Object> buymap = new HashMap<String, Object>();
		buymap.put("name", "采购费用");
		buymap.put("value", buyAmount);
		
		Map<String,Object> salemap = new HashMap<String, Object>();
		salemap.put("name", "销售费用");
		salemap.put("value", saleAmount);
		
		Map<String,Object> wastagemap = new HashMap<String, Object>();
		wastagemap.put("name", "损耗费用");
		wastagemap.put("value", wasteageAmount);
		
		Map<String,Object> totalmap = new HashMap<String, Object>();
		totalmap.put("name", "总计");
		totalmap.put("value", total);
		
		listMap.add(buymap);
		listMap.add(wastagemap);
		listMap.add(salemap);
		listMap.add(totalmap);
		
		String jsonString = ListAndJsonUtil.getJSONStringFromListMap(listMap);
		
		ajaxJson(jsonString);
	}
	
	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public Integer getShopID() {
		return shopID;
	}

	public void setShopID(Integer shopID) {
		this.shopID = shopID;
	}
	
	public static void main(String[] args) throws ParseException {
		Date date = DateUtils.getDateFromString("2008-02", "yyyy-MM");
		
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
	}
}
